package com.trkj.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.CopyemailDAO;
import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmailDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.OutboxDAO;
import com.trkj.dao.RecipientDAO;
import com.trkj.pojo.Copyemail;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Email;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Outbox;
import com.trkj.pojo.Recipient;
import com.trkj.vo.BirthdetailVo;
import com.trkj.vo.BirthempVo;
import com.trkj.vo.CopywriteusedVo;
import com.trkj.vo.EmpUseByemailVo;
/**
 * 
 * @author 23153黎凯
 *
 */
import com.trkj.vo.MakedEmailVo;
import com.trkj.vo.NewcopyemailVo;
import com.trkj.vo.NewemailVo;
import com.trkj.vo.ReserveVo;
import com.trkj.vo.TimeVo;
@Service
@Transactional
public class EmailService {
	@Autowired
	EmpDAO empDAO;
	
	@Autowired
	EmailDAO emailDAO;
	
	@Autowired
	CopyemailDAO copyemailDAO;
	
	@Autowired
	RecipientDAO recipientDAO;
	
	@Autowired
	OutboxDAO outboxDAO;
	
	@Autowired
	DeptDAO deptDAO;
	/**
	 * select empinfo by email
	 * @param empid
	 * @return
	 * @throws ParseException 
	 */
	public EmpUseByemailVo selectempinfoEmail(Integer empid) throws ParseException {
		Emp emp=empDAO.findOne(empid);
		EmpUseByemailVo empUseByemailVo=new EmpUseByemailVo();
		empUseByemailVo.setEmp(emp);
		int hour=(new Date()).getHours();
		String hours=null;
		if(hour<12) {
			hours="上午好，";
		}else if(hour>=12&&hour<18) {
			hours="下午好，";
		}else if(hour>=18&&hour<22) {
			hours="晚上好！";
		}else {
			hours="夜深了，注意休息！";
		}
		empUseByemailVo.setNum(notreservenum(empid));
		empUseByemailVo.setTimeVo(timethree());
		empUseByemailVo.setHour(hours);
		return empUseByemailVo;
	}
	
	/*处理今明后三天的日期*/
	public TimeVo timethree() throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");  
		Date today=new Date();
		String todaystring=format.format(today);
		Calendar cal = Calendar.getInstance();  
	    cal.setTime(format.parse(todaystring));  
	    cal.add(Calendar.DAY_OF_YEAR, +1);  
	    String tomorrow = format.format(cal.getTime());  
	    Calendar cal1 = Calendar.getInstance();  
	    cal1.setTime(format.parse(todaystring));  
	    cal1.add(Calendar.DAY_OF_YEAR, +2); 
	    String thethirdday=format.format(cal1.getTime());  
	    TimeVo timeVo=new TimeVo();
	    timeVo.setToday(todaystring);
	    timeVo.setTomorrow(tomorrow);
	    timeVo.setThethirdday(thethirdday);
	    return timeVo;
	}
	
	/**
	 * modify emp's email
	 * @param empid
	 * @param email
	 * @return
	 */
	public boolean modifyemail(Integer empid,String email) {
		Emp emp = empDAO.findOne(empid);
		if(email!=null) {
			
			List<Email> emails=emailDAO.selectemailbyreserveemail(empDAO.findOne(empid).getEmpEmail());			
			if(emails.size()!=0) {
				for (Email email2 : emails) {
					emailDAO.queryreservebyemailid(email2.getEmailId()).setEmailEmails(email);
				}
			}	
			List<Copyemail> copyemails=copyemailDAO.selectcopybyreserveemail(empDAO.findOne(empid).getEmpEmail());			
			if(copyemails.size()!=0) {
				for (Copyemail copyemail : copyemails) {
					copyemailDAO.selectcopybyemailidandempid(copyemail.getEmail().getEmailId(), empid).setCopyemailEmail(email);
				}
			}
			emp.setEmpEmail(email);
			return true;
		}
		return false;
	}
	
	/**
	 * 已发送查询
	 * @param empid
	 * @return
	 */
	public List<MakedEmailVo> querymakedemailb(Integer empid){
		String emailstate="已发送";
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		List<MakedEmailVo> makedEmailVos=new ArrayList<MakedEmailVo>();
		List<Email> emails=emailDAO.querymakedemail(empid,emailstate);
		for (Email email : emails) {
			/*已发送的普通邮件*/
			MakedEmailVo makedEmailVo=new MakedEmailVo();
			makedEmailVo.setName(email.getEmp().getEmpName());
			makedEmailVo.setReservename(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
			makedEmailVo.setEmailId(email.getEmailId());
			makedEmailVo.setStartdate(format.format(email.getEmailStartdate()));
			makedEmailVo.setEmailAnnex(email.getEmailAnnex());
			makedEmailVo.setEmailTheme(email.getEmailTheme());
			makedEmailVo.setEmailText(email.getEmailText());
			if(email.getEmailEnddate()==null) {
				makedEmailVo.setReservestate("未签收");
			}else {
				makedEmailVo.setReservestate("签收时间："+format.format(email.getEmailEnddate()));
			}
			makedEmailVo.setEmailState(email.getEmailState());
			makedEmailVo.setEmailtype("普通邮件");
			makedEmailVo.setCopynum(email.getCopyemails().size());
			String copylist="";
			List<Copyemail> copyemails=email.getCopyemails();
			for (Copyemail copyemail : copyemails) {
				copylist+=copyemail.getEmp().getEmpName();
			}
			makedEmailVo.setCopylist(copylist);
			makedEmailVos.add(makedEmailVo);
		}
		return makedEmailVos;
	}
	
	
	/**
	 * 发件人存入垃圾箱
	 * @param emailid
	 */
	public boolean updateemailstatebyemailid(Integer emailid) {
		String emailstate="存入垃圾箱";
		Email email=emailDAO.findOne(emailid);
		email.setEmailState(emailstate);
		return true;
	}
	
	/**
	 * 暂存箱查询
	 * @param empid
	 * @return
	 */
	public List<MakedEmailVo> querysavedemail(Integer empid){
		String emailstate="暂存中";
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		List<MakedEmailVo> makedEmailVos=new ArrayList<MakedEmailVo>();
		List<Email> emails=emailDAO.querymakedemail(empid,emailstate);
		for (Email email : emails) {
			/*已发送的普通邮件*/
			MakedEmailVo makedEmailVo=new MakedEmailVo();
			makedEmailVo.setName(email.getEmp().getEmpName());
			if(email.getEmailEmails()==null) {
				makedEmailVo.setReservename("暂存中");
			}else {
				makedEmailVo.setReservename(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
			}			
			makedEmailVo.setEmailId(email.getEmailId());
			makedEmailVo.setStartdate(format.format(email.getEmailStartdate()));
			makedEmailVo.setEmailAnnex(email.getEmailAnnex());
			makedEmailVo.setEmailTheme(email.getEmailTheme());
			makedEmailVo.setEmailText(email.getEmailText());
			makedEmailVo.setReservestate("暂存中");
			makedEmailVo.setEmailState(email.getEmailState());
			makedEmailVo.setEmailtype("普通邮件");
			makedEmailVo.setCopynum(email.getCopyemails().size());
			List<Copyemail> copyemails=email.getCopyemails();
			String copylist="";
			if(email.getCopyemails().size()==1) {
				for (Copyemail copyemail : copyemails) {
					copylist+=copyemail.getEmp().getEmpName();
				}
			}else {
				for (Copyemail copyemail : copyemails) {
					copylist+=copyemail.getEmp().getEmpName()+",";
				}
			}
			makedEmailVo.setCopylist(copylist);
			makedEmailVos.add(makedEmailVo);			
		}
		return makedEmailVos;
	}
	
	/**
	 * 我的暂存箱删除 邮箱表
	 * @param emailid
	 * @return
	 */
	public boolean savedeletedoemial(Integer emailid) {
		emailDAO.delete(emailid);
		return true;
	}
	
	/*我的暂存箱删除 抄送表*/
	public boolean savedeletedocopy(Integer emailid) {
		List<Copyemail> copyemails=copyemailDAO.findAll();
		for (Copyemail copyemail : copyemails) {
			if(copyemail.getEmail().getEmailId()==emailid) {
				copyemailDAO.delete(copyemail.getCopyemailId());
			}
		}
		return true;
	}
	
	/**
	 * 我的暂存箱删除 发件表
	 * @param emailid
	 * @return
	 */
	public boolean savedeletedoout(Integer emailid) {
		Outbox outboxcun=new Outbox();
		List<Outbox> outboxs=outboxDAO.findAll();
		for (Outbox outbox : outboxs) {
			if(outbox.getEmail().getEmailId()==emailid) {
				outboxcun=outbox;
			}
		}
		outboxDAO.delete(outboxcun.getOutboxId());
		return true;
	}
	
	/**
	 * 垃圾箱查询
	 * @param empid
	 * @return
	 */
	public List<MakedEmailVo> querydeletedemail(Integer empid){
		String emailstate="存入垃圾箱";
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		List<MakedEmailVo> makedEmailVos=new ArrayList<MakedEmailVo>();
		List<Email> emails=emailDAO.querymakedemail(empid,emailstate);
		for (Email email : emails) {
			/*已发送的普通邮件*/
			MakedEmailVo makedEmailVo=new MakedEmailVo();
			makedEmailVo.setName(email.getEmp().getEmpName());
			makedEmailVo.setReservename(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
			makedEmailVo.setEmailId(email.getEmailId());
			makedEmailVo.setStartdate(format.format(email.getEmailStartdate()));
			makedEmailVo.setEmailAnnex(email.getEmailAnnex());
			makedEmailVo.setEmailTheme(email.getEmailTheme());
			makedEmailVo.setEmailText(email.getEmailText());
			if(email.getEmailEnddate()==null) {
				makedEmailVo.setReservestate("未签收");
			}else {
				makedEmailVo.setReservestate("签收时间："+format.format(email.getEmailEnddate()));
			}
			makedEmailVo.setEmailState(email.getEmailState());
			makedEmailVo.setEmailtype("普通邮件");
			makedEmailVo.setCopynum(email.getCopyemails().size());
			String copylist=null;
			List<Copyemail> copyemails=email.getCopyemails();
			for (Copyemail copyemail : copyemails) {
				copylist+="<"+copyemail.getEmp().getEmpName()+">";
			}
			makedEmailVo.setCopylist(copylist);
			makedEmailVos.add(makedEmailVo);
			
		}
		return makedEmailVos;
	}
	
	
	/**
	 * 收件箱查询
	 * @return
	 */
	public List<ReserveVo> queryreservebyempid(Integer empid){
		/*根据员工号获取将要用于查询的收件邮箱*/
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		List<ReserveVo> reserveVos=new ArrayList<ReserveVo>();
		Emp emp=empDAO.findOne(empid);
		List<Email> emails=emailDAO.queryallemailbydatedesc();
		for (Email email : emails) {
			Date today=new Date();
			if(email.getEmailState()!="暂存中") {
				if(!email.getEmailStartdate().after(today)) {
					/*普通邮件*/
					if (email.getEmailEmails()!=null && email.getEmailEmails().equals(emp.getEmpEmail())) {
						ReserveVo reserveVo=new ReserveVo();
						if(outboxDAO.reserverisornotdeleted(email.getEmailId())!=null) {
							reserveVo.setMakedname(email.getEmp().getEmpName());
							reserveVo.setName(emp.getEmpName());
							reserveVo.setEmailId(email.getEmailId());
							reserveVo.setStartdate(format.format(email.getEmailStartdate()));
							if(email.getEmailEnddate()==null) {
								reserveVo.setEnddate("未签收");
							}else {
								reserveVo.setEnddate(format.format(email.getEmailEnddate()));
							}
							reserveVo.setReserveempid(empDAO.selectempbyemail(email.getEmailEmails()).getEmpId());
							reserveVo.setEmailtheme(email.getEmailTheme());
							reserveVo.setEmailtext(email.getEmailText());
							reserveVo.setEmailannx(email.getEmailAnnex());
							reserveVo.setType("普通邮件");
							reserveVos.add(reserveVo);
						}				
					}
					/*抄送邮件*/
					Copyemail copyemail=copyemailDAO.selectmycopeemail(emp.getEmpEmail(), email.getEmailId(),emp.getEmpId());
					if(copyemail!=null) {
						ReserveVo reserveVo=new ReserveVo();
						if(email.getEmp().getEmpName()!=emp.getEmpName()) {
							reserveVo.setMakedname(email.getEmp().getEmpName());
							reserveVo.setName(emp.getEmpName());
							reserveVo.setEmailId(email.getEmailId());
							reserveVo.setStartdate(format.format(email.getEmailStartdate()));			
							Recipient recipient=recipientDAO.selectrecipient(emp.getEmpId(), email.getEmailId());
							if(recipient==null) {
								reserveVo.setEnddate("未签收");
							}else {
								reserveVo.setEnddate("已签收");
							}
							reserveVo.setReserveempid(copyemail.getEmp().getEmpId());
							reserveVo.setEmailtheme(email.getEmailTheme());
							reserveVo.setEmailtext(email.getEmailText());
							reserveVo.setEmailannx(email.getEmailAnnex());
							reserveVo.setType("抄送邮件");
							reserveVos.add(reserveVo);
						}		
					}
				}
			}
			
					
		}
		return reserveVos;		
	}
	
	/*我的收件删除 普通收件 */	
	public boolean myreservedeletegen(Integer emailid,Integer empid) {
		Outbox outboxcun=new Outbox();
		List<Outbox> outboxs=outboxDAO.findAll();
		for (Outbox outbox : outboxs) {
			if(outbox.getEmail().getEmailId()==emailid) {
				outboxcun=outbox;
			}
		}
		outboxDAO.delete(outboxcun.getOutboxId());
		return true;
	}
	
	/*我的抄送邮件删除*/
	public boolean myreservedeletecopy(Integer emailid,Integer empid) {
		copyemailDAO.selectcopybyemailidandempid(emailid, empid).setCopyemailEmail(" ");
		return true;
	}
	
	/*普通收件箱的收件详情*/
	public ReserveVo findreservebyempidandtypeandemailid(Integer emailid,Integer empid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		ReserveVo reserveVo=new ReserveVo();
		Email email=emailDAO.queryreservebyemailidandemailemail(empDAO.findOne(empid).getEmpEmail(), emailid);
		reserveVo.setMakedname(email.getEmp().getEmpName());
		reserveVo.setName(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
		reserveVo.setReserveempid(empDAO.selectempbyemail(email.getEmailEmails()).getEmpId());
		reserveVo.setEmailId(email.getEmailId());
		reserveVo.setStartdate(format.format(email.getEmailStartdate()));
		if(email.getEmailEnddate()==null) {
			reserveVo.setEnddate("签收时间："+format.format(new Date()));
			emailDAO.generalemaildoreserve(new Date(), emailid);
		}else {
			reserveVo.setEnddate("签收时间："+format.format(email.getEmailEnddate()));
		}
		reserveVo.setEmailtheme(email.getEmailTheme());
		reserveVo.setEmailtext(email.getEmailText());
		reserveVo.setEmailannx(email.getEmailAnnex());
		reserveVo.setType("普通邮件");
		return reserveVo;
	}
	
	/*抄送邮件*/
	public ReserveVo findreservebyempidandemailidcope(Integer emailid,Integer empid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		ReserveVo reserveVo=new ReserveVo();
		Email email=emailDAO.queryreservebyemailid(emailid);
		reserveVo.setMakedname(email.getEmp().getEmpName());
		reserveVo.setName(empDAO.findOne(empid).getEmpName());
		reserveVo.setReserveempid(empid);
		reserveVo.setEmailId(email.getEmailId());
		reserveVo.setStartdate(format.format(email.getEmailStartdate()));
		if(recipientDAO.selectrecipient(empid, emailid)==null) {
			reserveVo.setEnddate("正在签收");
			Recipient recipient=new Recipient();
			recipient.setEmail(email);
			recipient.setEmp(empDAO.findOne(empid));
			recipientDAO.save(recipient);
		}else {
			reserveVo.setEnddate("已签收");
		}
		reserveVo.setEmailtheme(email.getEmailTheme());
		reserveVo.setEmailtext(email.getEmailText());
		reserveVo.setEmailannx(email.getEmailAnnex());
		reserveVo.setType("抄送邮件");
		return reserveVo;
	}
	
	/**
	 * 我的已发送详情
	 * @param emailid
	 * @return
	 */
	public MakedEmailVo mymakedemaildetail(Integer emailid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		Email email=emailDAO.queryreservebyemailid(emailid);
		MakedEmailVo makedEmailVo=new MakedEmailVo();
		makedEmailVo.setName(email.getEmp().getEmpName());
		makedEmailVo.setReservename(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
		makedEmailVo.setEmailId(email.getEmailId());
		makedEmailVo.setEmailState(email.getEmailState());
		if(email.getEmailEnddate()==null) {
			makedEmailVo.setReservestate("未签收");
		}else {
			makedEmailVo.setReservestate("签收时间："+format.format(email.getEmailEnddate()));
		}
		makedEmailVo.setStartdate(format.format(email.getEmailStartdate()));
		makedEmailVo.setEmailTheme(email.getEmailTheme());
		makedEmailVo.setEmailText(email.getEmailText());
		makedEmailVo.setEmailAnnex(email.getEmailAnnex());
		makedEmailVo.setEmailtype("普通邮件");
		makedEmailVo.setCopynum(email.getCopyemails().size());
		String copylist="";
		List<Copyemail> copyemails=email.getCopyemails();
		for (Copyemail copyemail : copyemails) {
			copylist+="<"+copyemail.getEmp().getEmpName()+">";
		}
		makedEmailVo.setCopylist(copylist);
		return makedEmailVo;
	}
	
	/**
	 * 暂存箱详情
	 * @param emailid
	 * @return
	 */
	public MakedEmailVo mysavedemaildetail(Integer emailid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		Email email=emailDAO.queryreservebyemailid(emailid);
		MakedEmailVo makedEmailVo=new MakedEmailVo();
		makedEmailVo.setName(email.getEmp().getEmpName());
		if(email.getEmailEmails()==null) {
			makedEmailVo.setReservename("");
		}else {
			makedEmailVo.setReservename(empDAO.selectempbyemail(email.getEmailEmails()).getEmpName());
		}		
		makedEmailVo.setEmailId(email.getEmailId());
		makedEmailVo.setEmailState(email.getEmailState());
		makedEmailVo.setReservestate("暂存中");
		makedEmailVo.setStartdate(format.format(email.getEmailStartdate()));
		makedEmailVo.setEmailTheme(email.getEmailTheme());
		makedEmailVo.setEmailText(email.getEmailText());
		makedEmailVo.setEmailAnnex(email.getEmailAnnex());
		makedEmailVo.setEmailtype("普通邮件");
		makedEmailVo.setCopynum(email.getCopyemails().size());
		String copylist="";
		List<Copyemail> copyemails=email.getCopyemails();
		if(email.getCopyemails().size()==1) {
			for (Copyemail copyemail : copyemails) {
				copylist+=copyemail.getEmp().getEmpName();
			}
		}else {
			for (Copyemail copyemail : copyemails) {
				copylist+=copyemail.getEmp().getEmpName()+",";
			}
		}
		makedEmailVo.setCopylist(copylist);
		return makedEmailVo;
	}
	
	/*垃圾箱恢复*/
	public boolean deleteback(Integer emailid) {
		emailDAO.findOne(emailid).setEmailState("已发送");
		return true;		
	}
	
	/*垃圾箱永久删除*/
	public boolean deletedelete(Integer emailid) {
		emailDAO.findOne(emailid).setEmailState("永久删除");
		return true ;
	}
	
	/*新建邮件时获取主收件人*/
	public List<NewemailVo> newemailVolist(Integer empid){
		List<Emp> emps=empDAO.findAll();
		List<NewemailVo> newemailVos=new ArrayList<NewemailVo>();
		for (Emp emp : emps) {
			if(emp.getEmpId()!=empid) {
				NewemailVo newemailVo=new NewemailVo();
				newemailVo.setEmpid(emp.getEmpId());
				newemailVo.setEmpname(emp.getEmpName());
				newemailVo.setPosition(emp.getPosition().getPositionName());
				newemailVos.add(newemailVo);
			}
		}
		return newemailVos;
	} 
	
	/*新邮件查询抄送人*/
	public List<NewcopyemailVo> newemailVocopylist(Integer empid){
		List<Dept> depts=deptDAO.findAll();
		List<NewcopyemailVo> newcopyemailVos=new ArrayList<NewcopyemailVo>();
		for (Dept dept : depts) {
			NewcopyemailVo newcopyemailVo=new NewcopyemailVo();
			newcopyemailVo.setDeptId(dept.getDeptId());
			newcopyemailVo.setDeptname(dept.getDeptName());
			List<Emp> emps=dept.getEmps();
			List<NewemailVo> newemailVos=new ArrayList<NewemailVo>();
			for (Emp emp : emps) {
				NewemailVo newemailVo=new NewemailVo();
				if(emp.getEmpId()!=empid) {
					newemailVo.setEmpid(emp.getEmpId());
					newemailVo.setEmpname(emp.getEmpName());
					newemailVo.setPosition(emp.getPosition().getPositionName());
					newemailVos.add(newemailVo);
				}
			}
			newcopyemailVo.setNewemailVos(newemailVos);
			newcopyemailVos.add(newcopyemailVo);
		}
		return newcopyemailVos;
	}
	
	/*查询今天生日的员工*/
	public List<BirthempVo> todaysbirthemp(Integer empid) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); 
		TimeVo timeVo=timethree();
		List<Emp> emps=empDAO.findAll();
		List<BirthempVo> birthempVos=new ArrayList<BirthempVo>();
		for (Emp emp : emps) {
			String birth=format.format(emp.getEmpBirthday());
			if(emp.getEmpId()!=empid) {
				if(timeVo.getToday().substring(5, 7).equals(birth.substring(5, 7))) {
					if(timeVo.getToday().substring(8, 10).equals(birth.substring(8,10))) {
						BirthempVo birthempVo=new BirthempVo();
						birthempVo.setEmpid(emp.getEmpId());
						birthempVo.setEmpname(emp.getEmpName());
						birthempVo.setPosition(emp.getPosition().getPositionName());
						birthempVos.add(birthempVo);					
					}		
				}						
			}
		}
		return birthempVos;
	}
	
	/*查询明天生日的员工*/
	public List<BirthempVo> tomorrowsbirthemp(Integer empid) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); 
		TimeVo timeVo=timethree();
		List<Emp> emps=empDAO.findAll();
		List<BirthempVo> birthempVos=new ArrayList<BirthempVo>();
		for (Emp emp : emps) {
			String birth=format.format(emp.getEmpBirthday());
			if(emp.getEmpId()!=empid) {
				if(timeVo.getTomorrow().substring(5, 7).equals(birth.substring(5, 7))) {
					if(timeVo.getTomorrow().substring(8, 10).equals(birth.substring(8,10))) {
						BirthempVo birthempVo=new BirthempVo();
						birthempVo.setEmpid(emp.getEmpId());
						birthempVo.setEmpname(emp.getEmpName());
						birthempVo.setPosition(emp.getPosition().getPositionName());
						birthempVos.add(birthempVo);					
					}		
				}						
			}
		}
		return birthempVos;
	}
	
	/*查询后天生日的员工*/
	public List<BirthempVo> thethirdbirthemp(Integer empid) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); 
		TimeVo timeVo=timethree();
		List<Emp> emps=empDAO.findAll();
		List<BirthempVo> birthempVos=new ArrayList<BirthempVo>();
		for (Emp emp : emps) {
			String birth=format.format(emp.getEmpBirthday());
			if(emp.getEmpId()!=empid) {
				if(timeVo.getThethirdday().substring(5, 7).equals(birth.substring(5, 7))) {
					if(timeVo.getThethirdday().substring(8, 10).equals(birth.substring(8,10))) {
						BirthempVo birthempVo=new BirthempVo();
						birthempVo.setEmpid(emp.getEmpId());
						birthempVo.setEmpname(emp.getEmpName());
						birthempVo.setPosition(emp.getPosition().getPositionName());
						birthempVos.add(birthempVo);					
					}		
				}						
			}
		}
		return birthempVos;
	}
	
	/*生日朋友详情*/
	public BirthdetailVo getbirthdetail(Integer empid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		Emp emp=empDAO.findOne(empid);
		BirthdetailVo birthdetailVo=new BirthdetailVo();
		birthdetailVo.setEmpid(emp.getEmpId());
		birthdetailVo.setEmpname(emp.getEmpName());
		birthdetailVo.setDept(emp.getDept().getDeptName());
		birthdetailVo.setPosition(emp.getPosition().getPositionName());
		if(emp.getEmpSex().equals("1")) {
			birthdetailVo.setSex("男");
		}else {
			birthdetailVo.setSex("女");
		}
		birthdetailVo.setTel(emp.getEmpPhone());
		birthdetailVo.setQq(emp.getEmp_QQ());
		birthdetailVo.setBirth(format.format(emp.getEmpBirthday()));
		birthdetailVo.setEmail(emp.getEmpEmail());
		return birthdetailVo;	
	}
	
	/*首页获取未签收的邮件*/
	public int notreservenum(Integer empid){
		/*根据员工号获取将要用于查询的收件邮箱*/
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
		Emp emp=empDAO.findOne(empid);
		int num=0;
		List<Email> emails=emailDAO.queryallemailbydatedesc();
		for (Email email : emails) {
			/*普通邮件*/
			if(email.getEmailStartdate().before(new Date())) {
				if (email.getEmailEmails()!=null && email.getEmailEmails().equals(emp.getEmpEmail())) {
					if(email.getEmailEnddate()==null) {
						num+=1;
					}				
				}
				/*抄送邮件*/
				Copyemail copyemail=copyemailDAO.selectmycopeemail(emp.getEmpEmail(), email.getEmailId(),emp.getEmpId());
				if(copyemail!=null) {
					if(email.getEmp().getEmpName()!=emp.getEmpName()) {						
						Recipient recipient=recipientDAO.selectrecipient(emp.getEmpId(), email.getEmailId());
						if(recipient==null) {
							num+=1;
						}
					}		
				}		
			}
			
		}
		return num;		
	}
	
	/*邮件处理抄送人并且发送*/
	public void postemailproccess(Integer empid,String name,String copy,String theme,String text) {
		String[] a=copy.split(",");
		List<CopywriteusedVo> copywriteusedVos=new ArrayList<CopywriteusedVo>();
		for(int i=0;i<a.length;i++) {
			if(!a[i].equals("采购部")) {
				if(!a[i].equals("人事部")) {
					if(!a[i].equals("财务部")) {
						if(!a[i].equals("总经办")) {
							if(!a[i].equals("市场部")) {
								CopywriteusedVo copywriteusedVo=new CopywriteusedVo();
								Emp emp=empDAO.selectempbyname(a[i]);
								copywriteusedVo.setEmpid(emp.getEmpId());
								copywriteusedVo.setEmpname(emp.getEmpName());
								copywriteusedVo.setEmpemail(emp.getEmpEmail());
								copywriteusedVos.add(copywriteusedVo);
							}
						}
					}
				}
			}	
		}
		/*以上获取了抄送人集合*/
		postemail( empid, name,copywriteusedVos, theme, text);/*跳到发送服务方法*/
	}
	
	/*发送邮件*/
	public void postemail(Integer empid,String name,List<CopywriteusedVo> copy,String theme,String text) {
		Emp emp=empDAO.selectempbyname(name);/*获取抄送人信息*/
		Emp maked=empDAO.findOne(empid);/*发送人maked对象*/
		String nameemail=emp.getEmpEmail();/*主收件人对象*/
		Email email=new Email();
		email.setEmp(maked);
		email.setEmailTheme(theme);
		email.setEmailStartdate(new Date());
		email.setEmailAnnex("无");
		email.setEmailText(text);
		email.setEmailState("已发送");
		email.setEmailEmails(nameemail);
		emailDAO.save(email);/*邮件表存储邮件对象*/
		/*List<Email> emails=emailDAO.queryallemailbydatedesc();*/
		Email emailtime=new Email();/*获取最新填入的邮件*/		
		List<Email> emailall=emailDAO.findAll();
		for (Email email2 : emailall) {
			if(email2.getOutbox()==null) {
				if(email2.getEmp().getEmpId()==empid) {
					emailtime=email2;
				}
			}
		}
		Outbox outbox=new Outbox();/*发件表存储一条记录*/
		outbox.setEmail(emailtime);
		outboxDAO.save(outbox);
		/*抄送邮件表循环存储数据*/
		List<CopywriteusedVo> copywriteusedVos=copy;
		for (CopywriteusedVo copywriteusedVo : copywriteusedVos) {
			Copyemail copyemail=new Copyemail();
			copyemail.setEmp(empDAO.findOne(copywriteusedVo.getEmpid()));
			copyemail.setEmail(emailtime);
			copyemail.setCopyemailEmail(copywriteusedVo.getEmpemail());
			copyemailDAO.save(copyemail);
		}
	}
	
	/*发送一封没有抄送人的邮件*/
	public void postemailwithoutcopy(Integer empid,String reserveemp,String theme,String text) {
		Emp emp=empDAO.selectempbyname(reserveemp);/*获取收件人信息*/
		Emp maked=empDAO.findOne(empid);/*发送人maked对象*/
		String nameemail=emp.getEmpEmail();/*主收件人对象*/
		Email email=new Email();
		email.setEmp(maked);
		email.setEmailTheme(theme);
		email.setEmailStartdate(new Date());
		email.setEmailAnnex("无");
		email.setEmailText(text);
		email.setEmailState("已发送");
		email.setEmailEmails(nameemail);
		
		emailDAO.save(email);/*邮件表存储邮件对象*/
		Email emailtime=new Email();/*获取最新填入的邮件*/		
		List<Email> emailall=emailDAO.findAll();
		for (Email email2 : emailall) {
			if(email2.getOutbox()==null) {
				if(email2.getEmp().getEmpId()==empid) {
					emailtime=email2;
				}
			}
		}
		Outbox outbox=new Outbox();/*发件表存储一条记录*/
		outbox.setEmail(emailtime);
		outboxDAO.save(outbox);
	}
	
	public String getempnamebyempid(Integer empid) {
		Emp emp=empDAO.findOne(empid);
		return emp.getEmpName();
	}
	
	/*邮件处理抄送人并且暂存*/
	public void savepostemailproccess(Integer empid,String name,String copy,String theme,String text) {
		String[] a=copy.split(",");
		List<CopywriteusedVo> copywriteusedVos=new ArrayList<CopywriteusedVo>();
		for(int i=0;i<a.length;i++) {
			if(!a[i].equals("采购部")) {
				if(!a[i].equals("人事部")) {
					if(!a[i].equals("财务部")) {
						if(!a[i].equals("总经办")) {
							if(!a[i].equals("市场部")) {
								CopywriteusedVo copywriteusedVo=new CopywriteusedVo();
								Emp emp=empDAO.selectempbyname(a[i]);
								copywriteusedVo.setEmpid(emp.getEmpId());
								copywriteusedVo.setEmpname(emp.getEmpName());
								copywriteusedVo.setEmpemail(emp.getEmpEmail());
								copywriteusedVos.add(copywriteusedVo);
							}
						}
					}
				}
			}	
		}
		/*以上获取了抄送人集合*/
		savepostemail( empid, name,copywriteusedVos, theme, text);/*跳到发送服务方法*/
	}
	
	/*暂存有抄送人的邮件*/
	public void savepostemail(Integer empid,String name,List<CopywriteusedVo> copy,String theme,String text) {
		Emp emp=empDAO.selectempbyname(name);/*获取抄送人信息*/
		Emp maked=empDAO.findOne(empid);/*发送人maked对象*/
		String nameemail=emp.getEmpEmail();/*主收件人对象*/
		Email email=new Email();
		email.setEmp(maked);
		email.setEmailTheme(theme);
		email.setEmailStartdate(new Date());
		email.setEmailAnnex("无");
		email.setEmailText(text);
		email.setEmailState("暂存中");
		email.setEmailEmails(nameemail);
		emailDAO.save(email);/*邮件表存储邮件对象*/
		/*List<Email> emails=emailDAO.queryallemailbydatedesc();*/
		Email emailtime=new Email();/*获取最新填入的邮件*/		
		List<Email> emailall=emailDAO.findAll();
		for (Email email2 : emailall) {
			if(email2.getOutbox()==null) {
				if(email2.getEmp().getEmpId()==empid) {
					emailtime=email2;
				}
			}
		}
		Outbox outbox=new Outbox();/*发件表存储一条记录*/
		outbox.setEmail(emailtime);
		outboxDAO.save(outbox);
		/*抄送邮件表循环存储数据*/
		List<CopywriteusedVo> copywriteusedVos=copy;
		for (CopywriteusedVo copywriteusedVo : copywriteusedVos) {
			Copyemail copyemail=new Copyemail();
			copyemail.setEmp(empDAO.findOne(copywriteusedVo.getEmpid()));
			copyemail.setEmail(emailtime);
			copyemail.setCopyemailEmail(copywriteusedVo.getEmpemail());
			copyemailDAO.save(copyemail);
		}
	}
	
	/*暂存一个没有抄送人的邮件*/
	public void savepostemailwithoutcopy(Integer empid,String reserveemp,String theme,String text) {
		Emp emp=empDAO.selectempbyname(reserveemp);/*获取收件人信息*/
		Emp maked=empDAO.findOne(empid);/*发送人maked对象*/
		String nameemail=emp.getEmpEmail();/*主收件人对象*/
		Email email=new Email();
		email.setEmp(maked);
		email.setEmailTheme(theme);
		email.setEmailStartdate(new Date());
		email.setEmailAnnex("无");
		email.setEmailText(text);
		email.setEmailState("暂存中");
		email.setEmailEmails(nameemail);
		
		emailDAO.save(email);/*邮件表存储邮件对象*/
		Email emailtime=new Email();/*获取最新填入的邮件*/		
		List<Email> emailall=emailDAO.findAll();
		for (Email email2 : emailall) {
			if(email2.getOutbox()==null) {
				if(email2.getEmp().getEmpId()==empid) {
					emailtime=email2;
				}
			}
		}
		Outbox outbox=new Outbox();/*发件表存储一条记录*/
		outbox.setEmail(emailtime);
		outboxDAO.save(outbox);
	}
	
	/**
	 * 发送暂存邮件
	 * @param emailid
	 * @param theme
	 * @param text
	 */
	public void saveforemail(Integer emailid,String theme,String text) {
		emailDAO.findOne(emailid).setEmailTheme(theme);
		emailDAO.findOne(emailid).setEmailText(text);
		emailDAO.findOne(emailid).setEmailState("已发送");
	}
	
	/**
	 * 暂存邮件保存编辑
	 * @param emailid
	 * @param theme
	 * @param text
	 */
	public void saveforsave(Integer emailid,String theme,String text) {
		emailDAO.findOne(emailid).setEmailTheme(theme);
		emailDAO.findOne(emailid).setEmailText(text);
	}
	
	/**
	 * 回复已收件
	 * @param empid
	 * @param name
	 * @param theme
	 * @param text
	 */
	public void forbackemail(Integer empid,String name,String theme,String text) {
		Email email=new Email();
		email.setEmp(empDAO.findOne(empid));
		email.setEmailTheme(theme);
		email.setEmailStartdate(new Date());
		email.setEmailText(text);
		email.setEmailAnnex("无");
		email.setEmailState("已发送");
		email.setEmailEmails(empDAO.selectempbyname(name).getEmpEmail());
		emailDAO.save(email);
		Email emailtime=new Email();/*获取最新填入的邮件*/		
		List<Email> emailall=emailDAO.findAll();
		for (Email email2 : emailall) {
			if(email2.getOutbox()==null) {
				if(email2.getEmp().getEmpId()==empid) {
					emailtime=email2;
				}
			}
		}
		Outbox outbox=new Outbox();/*发件表存储一条记录*/
		outbox.setEmail(emailtime);
		outboxDAO.save(outbox);
	}
}
